/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.general.bc;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.general.be.Area;
import org.cip.esymons.general.be.Cargo;
import org.cip.esymons.general.be.Empleado;

/**
 *
 * @author Freddy Infantes
 */
@Stateless
public class AreaFacade implements AreaFacadeLocal {

    @PersistenceContext
    private EntityManager em;

    public void create(Area area) {
        em.persist(area);
    }

    public void edit(Area area) {
        em.merge(area);
    }

    public void remove(Area area) {
        em.remove(em.merge(area));
    }

    public Area find(Object id) {
        return em.find(Area.class, id);
    }

    public List<Area> findAll() {
        return em.createQuery("select object(o) from Area as o order by o.areNombre").getResultList();
    }

    public Area buscar(String nom) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select a From Area a ");

            if (nom != null && !nom.isEmpty()) {
                sb.append(" Where a.areNombre = ?1");
            }
            q = em.createQuery(sb.toString());

            if (nom != null && !nom.isEmpty()) {
                q.setParameter(1, nom);
            }

            return (Area) q.getSingleResult();
        } catch (Exception ex) {
            return null;
        }
    }

    public List<Area> listarXEmpleado(Empleado empl) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("select c.cargo.fkAreCodigo from CargoEmpleado c inner Join c.empleado o");
            sb.append(" where o=?1 ");

            q = em.createQuery(sb.toString());

            q.setParameter(1, empl);

            return q.getResultList();
        } catch (Exception ex) {
            return null;
        }
    }

    public Area buscarXCargo(Cargo cargo) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("select a from Cargo c inner Join c.fkAreCodigo a");
            sb.append(" where c=?1 ");

            q = em.createQuery(sb.toString());

            q.setParameter(1, cargo);

            return (Area) q.getSingleResult();
        } catch (Exception ex) {
            return null;
        }
    }

    public List<Area> paginadorArea(int cantidad, int posicion, String nom) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select a From Area a ");

            if (nom != null && !nom.isEmpty()) {
                sb.append(" Where a.areNombre like ?1");
            }
            q = em.createQuery(sb.toString());

            if (nom != null && !nom.isEmpty()) {
                q.setParameter(1, "%" + nom + "%");
            }
            q.setFirstResult(posicion);
            q.setMaxResults(cantidad);
            return q.getResultList();
        } catch (Exception ex) {
            return new ArrayList();
        }
    }

    public int contarPaginadorArea(String nom) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select COUNT(a) From Area a ");

            if (nom != null && !nom.isEmpty()) {
                sb.append(" Where a.areNombre =?1");
            }
            q = em.createQuery(sb.toString());

            if (nom != null && !nom.isEmpty()) {
                q.setParameter(1, nom);
            }
            return new Integer(q.getSingleResult().toString());
        } catch (Exception ex) {
            return 0;
        }
    }
}
